import cn.yunhe.dao.FoodXiangQing;
import cn.yunhe.dao.IUserAddress;
import cn.yunhe.entity.merchantsentity.Food;
import cn.yunhe.entity.userentity.UserAccount;
import cn.yunhe.entity.userentity.UserAddress;
import cn.yunhe.entity.userentity.UserData;
import cn.yunhe.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Demo3 {
    SqlSession sqlSession= null;
    IUserAddress iUserAddress=null;
    FoodXiangQing foodXiangQing = null;
    @Before
    public void init(){
        sqlSession= SqlSessionFactoryUtil.getSqlSession();
        iUserAddress=sqlSession.getMapper(IUserAddress.class);
        foodXiangQing = sqlSession.getMapper(FoodXiangQing.class);
    }
    @Test
    public void test1(){
        UserAddress userAddress=new UserAddress();
        UserData userData=new UserData();
        UserAccount userAccount=new UserAccount(1);

        userData.setUserAccount(userAccount);
        userAddress.setUserData(userData);
        String address="河南省郑州市高新区";
        userAddress.setAddress(address);
        int flag=iUserAddress.addUserAddress(userAddress);
        sqlSession.commit();
        System.out.println(flag);
    }
    @Test
    public void test2(){
       List<UserAddress> userAddressList=iUserAddress.getUserAddressById(1);

        for (UserAddress  n:userAddressList) {
            System.out.println(n.getUserData().getNickname()+","
                    + n.getAddress());
        }

    }
    @Test
    public void test3(){
        UserAddress userAddress=new UserAddress();
        UserData userData=new UserData();
        UserAccount userAccount=new UserAccount(1);

        userData.setUserAccount(userAccount);
        userAddress.setUserData(userData);
        String address="河南省郑州市高新区";
        userAddress.setAddress(address);
        int flag=iUserAddress.deleteUserAddress(userAddress);
        sqlSession.commit();
        System.out.println(flag);
    }
    @Test
    public void test4(){
        UserAddress userAddress=new UserAddress();
        UserData userData=new UserData();
        UserAccount userAccount=new UserAccount(1);

        userData.setUserAccount(userAccount);
        userAddress.setUserData(userData);
        String address="河南省";
        userAddress.setAddress(address);

        String newAddress="北京市";

        Map map=new HashMap();
        map.put("userAddress",userAddress);
        map.put("newAddress",newAddress);
        int flag=iUserAddress.updateUserAddress(map);
        sqlSession.commit();
        System.out.println(flag);



    }

    @Test
    public void test5(){
        List<Food> foodList = foodXiangQing.getId(8);
        for(Food ff :foodList){
            System.out.println(ff.getName()+" "+ff.getPrice());
        }

    }





    @After
    public void close(){
        sqlSession.close();
    }
}
